package com.feib.soeasy.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.LocalizableMessage;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.ValidationErrorHandler;

import com.feib.soeasy.model.User;
import com.feib.soeasy.service.UserService;
import com.feib.soeasy.util.UserUtil;

/**
 * @title (#)Soez9010ActionBean.java<br>
 * @description 基本資料修改<br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/23 create by Anson Tsai<br>
 */
public class Soez9010ActionBean extends BaseActionBean implements ValidationErrorHandler {

    /**
     * 基本資料修改畫面
     */
    private static final String DEFAULT_JSP = "/WEB-INF/views/soez9010/soez9010.jsp";

    /**
     * 回應訊息
     */
    private String message;

    /**
     * 訊息抬頭
     */
    private String messageTitle;

    private User user;

    @SpringBean("userService")
    private UserService userService;

    @DontBind
    @DefaultHandler
    public Resolution doDefault() {
        logger.debug("Enter doDefault mehtod.");

        this.user = userService.queryByUerPk(UserUtil.getUser().getUserPk());

        return new ForwardResolution(DEFAULT_JSP);
    }

    /**
     * 
     * @return
     */
    public Resolution doSave() {
        logger.debug("Enter doSave mehtod.");
        try {
            User u = userService.queryByUerPk(user.getUserPk());
            u.setUserName(user.getUserName());
            u.setUserPhone(user.getUserPhone());
            u.setUserEmail(user.getUserEmail());
            u.setUserMemo(user.getUserMemo());
            user = userService.saveUser(u);

            message = new LocalizableMessage("Soez9010.action.doSaveBtn.success").getMessage(getContext().getLocale()); // 基本資料更新成功
            this.messageTitle = new LocalizableMessage("message.title").getMessage(getContext().getLocale()); // 訊息
        }
        catch (Exception e) {
            logger.error("基本資料修改錯誤(Soez9010ActionBean.doSaveBtn):", e);
            message = e.getMessage();
            this.messageTitle = new LocalizableMessage("error.title").getMessage(getContext().getLocale()); // 錯誤
        }
        return new ForwardResolution(DEFAULT_JSP);
    }

    /**
     * @return 傳回 user。
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user 要設定的 user。
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @return 傳回 message。
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message 要設定的 message。
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return 傳回 messageTitle。
     */
    public String getMessageTitle() {
        return messageTitle;
    }

    /**
     * @param messageTitle 要設定的 messageTitle。
     */
    public void setMessageTitle(String messageTitle) {
        this.messageTitle = messageTitle;
    }

}
